﻿using LiveCharts.Wpf;
using LiveCharts;
using Prism.Mvvm;
using Prism.Regions;
using RecordBill.Data;
using RecordBill.Dtos;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Prism.Commands;

namespace RecordBill.ViewModels
{
    internal class HomeViewModel :BindableBase, INavigationAware
    {
        private readonly ImportService _ser;
        private UISummaryStat _sumStat;
        public UISummaryStat Sumstat
        {
            get => _sumStat;
            set
            {
                _sumStat = value;
                RaisePropertyChanged();
            }
        }

        private PageQueryBill _query;
        public PageQueryBill Query
        {
            get => _query;
            set
            {
                _query = value;
                RaisePropertyChanged();
            }
        }
        private List<Transaction> _top5Trans;
        public List<Transaction> Top5Trans
        {
            get => _top5Trans;
            set
            {
                _top5Trans = value;
                RaisePropertyChanged();
            }
        }
        private SeriesCollection _seriesCollection;
        public SeriesCollection Series
        {
            get => _seriesCollection;
            set
            {
                _seriesCollection = value;
                RaisePropertyChanged();
            }
        }
        public DelegateCommand QueryBillCommand { get; set; }

        public HomeViewModel(ImportService ser)
        {
            Sumstat = new UISummaryStat();
            _ser = ser;
            var query = new PageQueryBill();
            var currentDate = DateTime.Now;
            // 获取当前日期的年份和月份
            int currentYear = currentDate.Year;
            int currentMonth = currentDate.Month;
            var preMonth = new DateTime(currentYear, currentMonth, 1).AddMonths(-1);
            // 计算上个月的最后一天
            DateTime lastDayOfPreviousMonth = preMonth.AddMonths(1).AddDays(-1);
            query.StartTime = preMonth;
            query.EndTime = lastDayOfPreviousMonth;
            this.Query = query;
            QueryBillCommand = new DelegateCommand(LoadBills);
        }

        private async void LoadBills()
        {
            if (Query.StartTime == null || Query.EndTime == null)
            {
                return;
            }
           var list= await _ser.QueryAllBillItems(Query);
            var preQuery= new PageQueryBill();
            preQuery.StartTime = Query.StartTime.Value.AddMonths(-1);
            preQuery.EndTime = Query.EndTime.Value.AddMonths(-1);
            var listPre = await _ser.QueryAllBillItems(preQuery);
            var listIncome = list.Where(a => a.IncomeOrExpense == "收入").ToList();
            var listExpense = list.Where(a => a.IncomeOrExpense == "支出").ToList();

            var preListIncome = listPre.Where(a => a.IncomeOrExpense == "收入").ToList();
            var preListExpense = listPre.Where(a => a.IncomeOrExpense == "支出").ToList();

            var sumIncome = listIncome.Sum(a => a.Amount);
            var sumExpense = listExpense.Sum(a => a.Amount);

            var preSumIncome = preListIncome.Sum(a => a.Amount);
            var preSumExpense = preListExpense.Sum(a => a.Amount);

            Sumstat.SumExpense = sumExpense;
            Sumstat.SumIncome= sumIncome;
            Sumstat.SumIncomeComparePreMonth = sumIncome - preSumIncome;
            Sumstat.SumExpenseComparePreMonth = sumExpense - preSumExpense;

            var listTop5=listExpense.OrderByDescending(a=>a.Amount).Take(5).ToList();
            Top5Trans = listTop5;

            LoadPieChart(list);
        }

        private  void LoadPieChart(List<Transaction> list)
        {
            SeriesCollection SeriesCollection = new SeriesCollection();

            var grops = list.GroupBy(a => a.StatisticsType).ToList();

            foreach (var item in grops)
            {
                SeriesCollection.Add(new PieSeries()
                {
                    Title = item.Key,
                    Values = new ChartValues<Decimal>() { item.ToList().Sum(a => a.Amount) },
                    DataLabels = true,
                    LabelPoint = new Func<ChartPoint, string>((chartPoint) =>
                    {
                        return string.Format("{0}{1} ({2:P})", chartPoint.SeriesView.Title, chartPoint.Y, chartPoint.Participation);
                    })
                });
            }
            Series=SeriesCollection;
        }


        public bool IsNavigationTarget(NavigationContext navigationContext)
        {
            return true;
        }

        public void OnNavigatedFrom(NavigationContext navigationContext)
        {
            
        }

        public void OnNavigatedTo(NavigationContext navigationContext)
        {
            LoadBills();
        }
    }
}
